package com.hllxd.hism.passport.login.gateway.service;

import com.alibaba.fastjson.JSONObject;
import com.hllxd.hism.passport.login.gateway.util.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * author: xiyong.lxy
 * date: 2019-7-22 18:04
 */


@Component
public class MessageSender {
    private static Logger logger = LoggerFactory.getLogger(MessageSender.class);
    @Value("${sms.account_no:6571688575832698900}")
    private String account_no;
    private static String account_no_switch;
//    @Resource(
//        name = "hualalaMessageSenderClient"
//    )
//    private GrpcClient hualalaMessageSenderClient;
//    private static GrpcClient hualalaMessageSenderClientStatic;

    public MessageSender() {
    }

    @PostConstruct
    public void init() {
//        hualalaMessageSenderClientStatic = this.hualalaMessageSenderClient;
        account_no_switch = this.account_no;
    }

//    static MessageSendServiceFutureStub getMessageSenderFutureStub() throws Exception {
//        return (MessageSendServiceFutureStub)hualalaMessageSenderClientStatic.getFutureStub(MessageSendServiceGrpc.class);
//    }

    public static JSONObject sendMessage(String toMobile, String messageContent, String bizSrc, String sysCode) {
        try {
//            MessageSendServiceFutureStub e = getMessageSenderFutureStub();
//            Builder request = MessageCustomSendReq.newBuilder();
//            request.setAccountNo(account_no_switch).setBizSrc(bizSrc).setSysCode(sysCode).setMessageType(MessageTypeEnum.MESSAGE_CAPTCHA.getValue());
//            request.setToMobile(toMobile).setGroupID(0L).setShopID(0L);
//            request.setMessageContent(messageContent);
//            request.setStartSendTime(0L);
//            request.setPriorityLevel(5);
//            request.setEffectiveTimeLen(0);
//            MessageCustomSendRes response = (MessageCustomSendRes)e.messageCustomSend(request.build()).get();
//            JSONObject result = new JSONObject();
//            result.put("success", Boolean.valueOf(MessageEnum.SUCCESS.getCode().equals(response.getResult().getCode())));
//            result.put("msg", response.getResult().getMessage());
//            result.put("code", response.getResult().getCode());
//            return result;
            return null;
        } catch (Exception var8) {
            logger.error("sendMessage error: {}", var8.toString());
            return ResultUtils.gRPCErrorResult();
        }
    }

    public static JSONObject sendMessage(String accountNo, String toMobile, String messageContent, String bizSrc, String sysCode) {
        try {
//            MessageSendServiceFutureStub e = getMessageSenderFutureStub();
//            Builder request = MessageCustomSendReq.newBuilder();
//            request.setAccountNo(accountNo).setBizSrc(bizSrc).setSysCode(sysCode).setMessageType(MessageTypeEnum.MESSAGE_CAPTCHA.getValue());
//            request.setToMobile(toMobile).setGroupID(0L).setShopID(0L);
//            request.setMessageContent(messageContent);
//            request.setStartSendTime(0L);
//            request.setPriorityLevel(5);
//            request.setEffectiveTimeLen(0);
//            MessageCustomSendRes response = (MessageCustomSendRes)e.messageCustomSend(request.build()).get();
//            JSONObject result = new JSONObject();
//            result.put("success", Boolean.valueOf(MessageEnum.SUCCESS.getCode().equals(response.getResult().getCode())));
//            result.put("msg", response.getResult().getMessage());
//            result.put("code", response.getResult().getCode());
//            return result;
            return null;
        } catch (Exception var9) {
            logger.error("sendMessage error: ", var9);
            return ResultUtils.gRPCErrorResult();
        }
    }

    public static JSONObject batchSendMessage(List<String> toMobiles, String messageContent, String bizSrc, String sysCode) {
        try {
//            MessageSendServiceFutureStub e = getMessageSenderFutureStub();
//            com.missfresh.app.message.sender.grpc.MessageSendData.MessageBatchSendReq.Builder request = MessageBatchSendReq.newBuilder();
//            request.setAccountNo(account_no_switch).setBizSrc(bizSrc).setSysCode(sysCode).setMessageType(MessageTypeEnum.MESSAGE_PROMOTION.getValue()).setSendType(
//                MessageSendTypeEnum.BATCH_SEND.getValue()).setGroupID(0L).setShopID(0L).setMessageContent(messageContent).setStartSendTime(0L).setPriorityLevel(5).setEffectiveTimeLen(0);
//            if(toMobiles != null && toMobiles.size() > 0) {
//                request.addAllToMobiles(toMobiles);
//            }
//
//            MessageBatchSendRes response = (MessageBatchSendRes)e.messageBatchSend(request.build()).get();
//            JSONObject result = new JSONObject();
//            result.put("success", Boolean.valueOf(MessageEnum.SUCCESS.getCode().equals(response.getResult().getCode())));
//            result.put("msg", response.getResult().getMessage());
//            result.put("code", response.getResult().getCode());
//            return result;
            return null;
        } catch (Exception var8) {
            logger.error("batchSendMessage error: ", var8);
            return ResultUtils.gRPCErrorResult();
        }
    }
}
